
/* 自定义dataSource 钩子函数 */
import { useEffect, useState } from 'react'
import http from '../../../util/http'
import { notification } from "antd"
import { BulbFilled } from "@ant-design/icons"
function usePublic(type) {
    const { username } = JSON.parse(localStorage.getItem("token"))
    const [dataSource, setdataSource] = useState([])
    useEffect(() => {
        http.get(`/news?author=${username}&publishState=${type}&_expand=category`).then(res => {
            setdataSource(res.data)
        })
    }, [username])
    const handleDown = (id) => {
        setdataSource(dataSource.filter(data => data.id !== id))
        http.patch(`/news/${id}`, {
            publishState: 3,
        }).then(res => {
            notification.info({
                message: `通知`,
                description:
                    `您可以到【已下线】中查看你的新闻新闻状态`,
                placement: "bottomRight",
                style: {
                    color: '#33ff00'
                },
                icon: <BulbFilled style={{ color: '#ffff33' }} />
            });
        })
    }
    const handleDel = (id) => {
        setdataSource(dataSource.filter(data => data.id !== id))
        http.delete(`/news/${id}`).then(res => {
            notification.info({
                message: `通知`,
                description:
                    `您已经删除了你的新闻`,
                placement: "bottomRight",
                style: {
                    color: '#33ff00'
                },
                icon: <BulbFilled style={{ color: '#ffff33' }} />
            });
        })
    }
    const handlePublish = (id) => {
        setdataSource(dataSource.filter(data => data.id !== id))
        http.patch(`/news/${id}`, {
            publishState: 2,
            publishTime: Date.now()
        }).then(res => {
            notification.info({
                message: `通知`,
                description:
                    `您可以到【发布管理/已发布】中查看你的新闻新闻状态`,
                placement: "bottomRight",
                style: {
                    color: '#33ff00'
                },
                icon: <BulbFilled style={{ color: '#ffff33' }} />
            });
        })
    }
    return {
        dataSource,
        handleDel,
        handleDown,
        handlePublish
    }
}
export default usePublic